package com.hjm.project.agentPosOperation.service.impl;

import java.io.IOException;
import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.FileOrImgUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.AgentMarketingReward.mapper.AgentMarketingRewardMapper;
import com.hjm.project.sysActConfig.domain.ActConfig;
import com.hjm.project.sysActConfig.mapper.ActConfigMapper;
import com.hjm.project.team.teamYlInfo.domain.TeamYlInfoDo;
import com.hjm.project.team.teamYlInfo.mapper.TeamYlInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.agentPosOperation.mapper.AgentPosOperationMapper;
import com.hjm.project.agentPosOperation.domain.AgentPosOperation;
import com.hjm.project.agentPosOperation.service.IAgentPosOperationService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 机具采购、退款Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-04-07
 */
@Service
public class AgentPosOperationServiceImpl implements IAgentPosOperationService 
{
    @Resource
    private AgentPosOperationMapper agentPosOperationMapper;
    @Resource
    private AgentMarketingRewardMapper agentMarketingRewardMapper;
    @Resource
    private TeamYlInfoMapper teamYlInfoMapper;                  //团员信息表
    @Resource
    private ActConfigMapper actConfigMapper;                    //活动期限

    /**
     * 查询机具采购、退款
     * 
     * @param id 机具采购、退款ID
     * @return 机具采购、退款
     */
    @Override
    public AgentPosOperation selectAgentPosOperationById(Integer id)
    {
        return agentPosOperationMapper.selectAgentPosOperationById(id);
    }

    /**
     * 查询机具采购、退款列表
     * 
     * @param agentPosOperation 机具采购、退款
     * @return 机具采购、退款
     */
    @Override
    public List<AgentPosOperation> selectAgentPosOperationList(AgentPosOperation agentPosOperation)
    {

        List<AgentPosOperation> agentPosOperations = agentPosOperationMapper.selectAgentPosOperationList(agentPosOperation);
        for (AgentPosOperation posOperation : agentPosOperations) {
            if (StringUtils.isNotEmpty(posOperation.getImageUrl())) {
                posOperation.setImageUrl(CustomConfig.getImgUrl() + posOperation.getImageUrl());
            }
        }
        return agentPosOperations;
    }

    /**
     * 新增机具采购、退款
     * 
     * @param agentPosOperation 机具采购、退款
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAgentPosOperation(MultipartFile pzImg,AgentPosOperation agentPosOperation) {
        //活动是否过期
        ActConfig actConfig = new ActConfig();
        actConfig.setType(1);
        List<ActConfig> actConfigs = actConfigMapper.selectActConfigList(actConfig);
        if (CollectionUtils.isEmpty(actConfigs)) {
            throw new BusinessException("请先配置活动期限");
        }
        if (DateUtils.daysBetween(DateUtils.getDate(),actConfigs.get(0).getEndDate()) < 0) {
            throw new BusinessException("当前活动已过期,不可配置");
        }
        //购机展业端管理
        String topAgentNum = agentPosOperation.getTopAgentNum();

        //查看购机展业端管理是否是团长或者团成员
        //如果是团长反4块 如果是团员 那么这4块 给他的团长
        TeamYlInfoDo teamYlInfoDo = new TeamYlInfoDo();
        teamYlInfoDo.setAgentNum(topAgentNum);
        teamYlInfoDo.setStatus("1");
        List<TeamYlInfoDo> teamYlInfoDos = teamYlInfoMapper.selectTeamYlInfoList(teamYlInfoDo);
        //不是团长
        if (CollectionUtils.isNotEmpty(teamYlInfoDos)) {
            //如果是团长那么存入团长ID 如果是团员那么存入所属团长ID
            TeamYlInfoDo teamYlInfoDo1 = teamYlInfoDos.get(0);
            //团长
            if (teamYlInfoDo1.getYlType().equals("1")) {
                agentPosOperation.setReturnAgentT(teamYlInfoDo1.getYlId().toString());
            }

            //团员
            if (teamYlInfoDo1.getYlType().equals("2")) {
                agentPosOperation.setReturnAgentT(teamYlInfoDo1.getParentYlId().toString());
            }
        }


        //返现展业端管理
        String agentNum = agentPosOperation.getAgentNum();
        //验证当前返现代理是否配置奖励信息
        //如果没有配置默认反购机展业端管理
        /*AgentMarketingReward marketingReward = new AgentMarketingReward();
        marketingReward.setStatus("1");
        marketingReward.setStart(DateUtils.getDate());
        marketingReward.setAgentNum(agentNum);
        marketingReward.setTopAgentNum(topAgentNum);
        List<AgentMarketingReward> agentMarketingRewards = agentMarketingRewardMapper.selectAgentMarketingRewardList(marketingReward);*/
        //反最上级
        if (StringUtils.isEmpty(agentNum)) {
            agentPosOperation.setReturnAgent(agentPosOperation.getTopAgentNum());
            agentPosOperation.setReturnAgentName(agentPosOperation.getTopAgentName());
        } else {
            //反目标展业端管理
            agentPosOperation.setReturnAgent(agentPosOperation.getAgentNum());
            agentPosOperation.setReturnAgentName(agentPosOperation.getAgentNum());
        }

        //先上传打款凭证,没有凭证或者失败直接返回
        String url = null;
        try {
            url = FileOrImgUtils.uploadImg(pzImg);
        } catch (Exception e) {
           throw  new BusinessException("上传图片异常,请稍后再试");
        }

        //上传失败
        if (StringUtils.isEmpty(url)) {
            throw  new BusinessException("上传图片失败,请稍后再试");
        }
        agentPosOperation.setAgentName(agentPosOperation.getTopAgentName());
        agentPosOperation.setAgentNum(agentPosOperation.getTopAgentNum());
        agentPosOperation.setImageUrl(url);
        agentPosOperation.setAddMan(ShiroUtils.getSysUser().getLoginName());
        agentPosOperation.setType("0");
        agentPosOperation.setSerial(DateUtils.getSimpleDateTimeMillisecond());
        if ("1".equals(agentPosOperation.getRewardType())) {
            agentPosOperation.setReturnMoneyT(String.valueOf((Integer.parseInt(agentPosOperation.getNum())* 4)));
        } else if ("2".equals(agentPosOperation.getRewardType())){
            agentPosOperation.setReturnMoney(String.valueOf((Integer.parseInt(agentPosOperation.getNum()) * 8)));
        }else if ("3".equals(agentPosOperation.getRewardType())){
            agentPosOperation.setReturnMoney(String.valueOf((Integer.parseInt(agentPosOperation.getNum()) * 2)));
        }
        int i = agentPosOperationMapper.insertAgentPosOperation(agentPosOperation);
        if (i <= 0) {
            FastDFSClient.deleteFile(url);
        }
        return i;
    }

    /**
     * 修改机具采购、退款
     * 
     * @param agentPosOperation 机具采购、退款
     * @return 结果
     */
    @Override
    public int updateAgentPosOperation(AgentPosOperation agentPosOperation)
    {
        return agentPosOperationMapper.updateAgentPosOperation(agentPosOperation);
    }

    /**
     * 删除机具采购、退款对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentPosOperationByIds(String ids)
    {
        return agentPosOperationMapper.deleteAgentPosOperationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机具采购、退款信息
     * 
     * @param id 机具采购、退款ID
     * @return 结果
     */
    @Override
    public int deleteAgentPosOperationById(Integer id)
    {
        return agentPosOperationMapper.deleteAgentPosOperationById(id);
    }
}
